package com.sugon.api;

import com.sugon.annotation.AuthCheck;
import com.sugon.entity.DevPlanGcEntity;
import com.sugon.entity.ExcepRecordEntity;
import com.sugon.entity.FaultTypeEntity;
import com.sugon.entity.MaintainPlanEntity;
import com.sugon.service.*;
import com.sugon.util.ApiBaseAction;
import com.sugon.util.ApiPageUtils;
import com.sugon.utils.Query;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "维护工单接口")
@RestController
@RequestMapping("/api/maintainplan")
public class ApiDevMaintainPlanController extends ApiBaseAction {

    @Autowired
    private MaintainPlanService maintainPlanService;
    @Autowired
    private FaultTypeService faultTypeService;
    @Autowired
    private DevPlanGcService devPlanGcService;
    @Autowired
    private DevPlanGcRecordService devPlanGcRecordService;
    @Autowired
    private ExcepRecordService excepRecordService;
    /**
     * 维护工单查询列表接口
     *
     * @param page
     * @param size
     * @return
     */
    @AuthCheck
    @ApiOperation(value = "维护工单查询列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", paramType = "query",dataType = "Integer",required = true),
            @ApiImplicitParam(name = "size", value = "每页记录数", paramType = "query",dataType = "Integer", required = true)
    })
    @RequestMapping(value = "getMaintainPlanList", method = RequestMethod.POST)
    public Object getMaintainPlanList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                        @RequestParam(value = "size", defaultValue = "10") Integer size) {

        Map<String, Object> params = new HashMap<>();
        params.put("page", page);
        params.put("limit", size);
        params.put("sidx","CREATE_DATE");
        params.put("order", "DESC");

        Query query = new Query(params);


        List<DevPlanGcEntity> devPlanGcList = devPlanGcService.queryList(query);
        int total = devPlanGcService.queryTotal(query);
        ApiPageUtils pageUtil = new ApiPageUtils(devPlanGcList, total, query.getLimit(), query.getPage());
        return toResponsSuccess(pageUtil);

    }

    /**
     * 维护工单查询详情
     * @param id
     * @return
     */
    @AuthCheck
    @ApiOperation(value = "维护工单查询详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "query",dataType = "String",required = true)
    })
    @RequestMapping(value = "getDetailById", method = RequestMethod.POST)
    public Object getDetailById(@RequestParam(value = "id") String  id) {
        MaintainPlanEntity maintainPlanEntity = maintainPlanService.queryObject(id);
        return toResponsSuccess(maintainPlanEntity);
    }

    /**
     * 维护工单上报
     * @param maintainPlan
     * @return
     */
    @AuthCheck
    @ApiOperation(value = "维护工单上报接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "userId", paramType = "query",dataType = "String",required = true)
    })
    @RequestMapping(value = "saveDevMaintainWork", method = RequestMethod.POST)
    public Object saveDevMaintainWork(@RequestBody MaintainPlanEntity maintainPlan,
                                      @RequestParam(value = "userId",required = false) String userId){
        maintainPlanService.saveApi(maintainPlan,userId);

        Map<String, Object> data = new HashMap<>();
        data.put("maintainPlan",maintainPlan);
        data.put("userId",userId);
        return toResponsSuccess(data);

    }
    @AuthCheck
    @ApiOperation(value = "故障类型查询")
    @RequestMapping(value = "select", method = RequestMethod.POST)
    public Object select(){
        Map<String, Object> params =new HashMap<>();
        params.put("category","1");
        List<FaultTypeEntity> faultList = faultTypeService.queryList(params);
        Map<String, Object> data = new HashMap<>();
        data.put("faultList",faultList);
        return toResponsSuccess(data);
    }

    @AuthCheck
    @ApiOperation(value = "维护项查看")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", paramType = "query",dataType = "Integer",required = true),
            @ApiImplicitParam(name = "size", value = "每页记录数", paramType = "query",dataType = "Integer", required = true),
            @ApiImplicitParam(name = "planId", value = "规程工单ID", paramType = "query",dataType = "String", required = true),
    })
    @RequestMapping(value = "detailsPlan", method = RequestMethod.POST)
    public Object detailsPlan(@RequestParam(value = "page", defaultValue = "1") Integer page,
                          @RequestParam(value = "size", defaultValue = "10") Integer size,
                          @RequestParam(value = "planId",required = false) String planId){

        Map<String, Object> params = new HashMap<>();
        params.put("page", page);
        params.put("limit", size);
        params.put("planId", planId);
        Query query = new Query(params);
        List<Map<String,Object>> maintainPlanEntity = maintainPlanService.getDetailsInfo(query);
        int total = maintainPlanService.queryTotal(query);
        ApiPageUtils pageUtil = new ApiPageUtils(maintainPlanEntity, total, query.getLimit(), query.getPage());
        return toResponsSuccess(pageUtil);
    }

    @AuthCheck
    @ApiOperation(value = "维护工单选项--批量结束")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "idsStr", value = "维护工单记录id", paramType = "query",dataType = "String",required = true)
    })
    @RequestMapping(value = "batchEnd", method = RequestMethod.POST)
    public Object BatchEnd(@RequestParam(value = "idsStr",required = false) String idsStr){
        int batchEnd = devPlanGcRecordService.batchEndApi(idsStr);
        Map<String, Object> data = new HashMap<>();
        data.put("batchEnd",batchEnd);
        return toResponsSuccess(data);
    }

    @AuthCheck
    @ApiOperation(value = "异常查看")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", paramType = "query",dataType = "Integer",required = true),
            @ApiImplicitParam(name = "size", value = "每页记录数", paramType = "query",dataType = "Integer", required = true),
            @ApiImplicitParam(name = "inspectionRecords", value = "所属记录", paramType = "query",dataType = "String", required = true),
            @ApiImplicitParam(name = "leader", value = "是否是领导 0否 1是", paramType = "query",dataType = "Integer", required = true),
            @ApiImplicitParam(name = "deptId", value = "部门id", paramType = "query",dataType = "Integer", required = true),
    })
    @RequestMapping(value = "details", method = RequestMethod.POST)
    public Object ExceprecordList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                          @RequestParam(value = "size", defaultValue = "10") Integer size,
                          @RequestParam(value = "inspectionRecords",required = false) String inspectionRecords,
                                  @RequestParam(value = "leader",required = false) Integer leader,
                                  @RequestParam(value = "deptId",required = false) String deptId){

        Map<String, Object> params = new HashMap<>();
        params.put("page", page);
        params.put("limit", size);
        params.put("inspectionRecords", inspectionRecords);
        params.put("leader", leader);
        params.put("deptId", deptId);
        Query query = new Query(params);

        List<ExcepRecordEntity> excepRecordList = excepRecordService.queryListApi(query,inspectionRecords,leader,deptId);
        int total = excepRecordService.queryTotal(query);
        ApiPageUtils pageUtil = new ApiPageUtils(excepRecordList, total, query.getLimit(), query.getPage());
        return toResponsSuccess(pageUtil);
    }

    /**
     * 维护工单异常上报
     * @param
     * @return
     */
    @AuthCheck
    @ApiOperation(value = "维护工单异常上报接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "inspectionRecords", value = "所属记录", paramType = "query",dataType = "String",required = true),
            @ApiImplicitParam(name = "excepCon", value = "异常现象", paramType = "query",dataType = "String", required = true),
            @ApiImplicitParam(name = "excepDescr", value = "问题描述", paramType = "query",dataType = "String", required = true),
            @ApiImplicitParam(name = "advice", value = "建议", paramType = "query",dataType = "String", required = true),
            @ApiImplicitParam(name = "remark ", value = "备注", paramType = "query",dataType = "String", required = true),
            @ApiImplicitParam(name = "userId", value = "用户id", paramType = "query",dataType = "String", required = true)
    })
    @RequestMapping(value = "error", method = RequestMethod.POST)
    public Object error(@RequestParam(value = "inspectionRecords",required = false) String inspectionRecords,
                                        @RequestParam(value = "excepCon",required = false) String excepCon,
                                        @RequestParam(value = "excepDescr",required = false) String excepDescr,
                                        @RequestParam(value = "advice",required = false) String advice,
                                        @RequestParam(value = "remark",required = false) String remark,
                                        @RequestParam(value = "userId",required = false) String userId
    ){

        ExcepRecordEntity excepRecordEntity = new ExcepRecordEntity();
        excepRecordEntity.setDevId(inspectionRecords);
        excepRecordEntity.setExcepCon(excepCon);
        excepRecordEntity.setExcepDescr(excepDescr);
        excepRecordEntity.setAdvice(advice);
        excepRecordEntity.setRemark(remark);
        excepRecordEntity.setCreateUser(userId);
        excepRecordService.saveApi(excepRecordEntity);
        return toResponsSuccess(excepRecordEntity);


    }

}
